package com.hzgj.bcl.soa.command;

import com.hzgj.bcl.redis.RedisClient;
import com.hzgj.bcl.util.IocContext;
import com.hzgj.bcl.util.lang.StrKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Tuple;

import java.util.Calendar;
import java.util.Set;

/**
 * Created by Administrator on 2016/9/13.
 */
public abstract class InvokeCommand extends BaseCommand {

    protected RedisClient redisClient;
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public String invoke(CommandOptions commandOptions) {
        String[] service_param = commandOptions.params.get("service");
        String[] method_param = commandOptions.params.get("method");
        String[] start_param = commandOptions.params.get("start");
        String[] end_param = commandOptions.params.get("end");
        String[] hostPort_param = commandOptions.params.get("hostPort");
        String service;
        if (service_param == null) {
            throw new RuntimeException("can't get parameter service.");
        }
        service = service_param[0];

        String method;
        if (method_param == null) {
            throw new RuntimeException("can't get parameter service.");
        }
        method = method_param[0];

        long start, end;
        if (start_param != null && end_param != null) {
            start = Long.parseLong(start_param[0]);
            end = Long.parseLong((end_param[0]));
        } else if (start_param == null && end_param != null) {
            end = Long.parseLong((end_param[0]));
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.HOUR, -2);
            start = calendar.getTimeInMillis();
        } else {
            long[] time = getCurrentTimeSpan();
            start = time[0];
            end = time[1];
        }
        String hostPort = null;
        if (hostPort_param != null) {
            hostPort = hostPort_param[0];
        }

        String baseKey = service + "_" + method;
        if (StrKit.notBlank(hostPort)) {
            baseKey += "_" + hostPort.replace(":", "_");
        }
        return handleNode(baseKey, start, end);
    }

    private long[] getCurrentTimeSpan() {
        Calendar calendar = Calendar.getInstance();
        long end = calendar.getTimeInMillis();
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        return new long[]{calendar.getTimeInMillis(), end};
    }

    abstract String handleNode(String baseKey, long start, long end);


    @Override
    public void start() {
        redisClient = IocContext.applicationContext.getBean(RedisClient.class);
    }


    protected String buildTuple(Set<Tuple> tuples) {
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        tuples.forEach(tuple -> {
            String member = tuple.getElement();
            builder.append("[").append((long) tuple.getScore()).append(",").append(member.split("_")[0]).append("]").append(",");
        });
        if (builder.length() > 1) {
            builder.deleteCharAt(builder.length() - 1);
        }
        builder.append("]");
        return builder.toString();
    }

}
